<template>
	<view>
		<view class="address-window" :class="isSign==true?'on':''">
			<view class='title'>
				<view class="iconfont left" style="left: 15px;">
					<u-icon name="checkmark" color="#888" size="20" @click='agree()'></u-icon>
				</view>
				签署协议
				<view class="iconfont">
					<u-icon name="close" color="#888" size="20" @click='close'></u-icon>
				</view>
			</view>
			<view class='box'>
				<view class="txt">用户在仔细阅读并同意所有服务条款完成注册程序后即时成为平台的正式用户。本注册协议自用户注册成功之日起，在用户与平台之间发生法律效力。</view>
				<view v-if="!signImg" class="sign" @click="sign()">签字区</view>
				<view v-else @click="sign()">
					<image class="signimg" :src="signImg" mode="widthFix" />
				</view>

			</view>
		</view>
		<view class='mask' catchtouchmove="true" :hidden='isSign==false' @click='close'></view>
	</view>
</template>

<script>
	import {
		uploadImage
	} from "../../api/common.js"
	import {
		uploadBase64Image,
		agreeSign
	} from "@/api/my.js";
	import {
		IMG_URL,
	} from "../../config/app.js";
	export default {
		props: {
			isSign: {
				type: Boolean,
				default: true,
			},
			shipid: {
				type: Number,
				default: 0
			},
		},
		data() {
			return {
				IMG_URL: IMG_URL,
				signImg: ''
			};
		},
		created() {
			let that = this
			uni.$on('isRefresh', function(data) {
				that.signImg = data
			})
		},
		methods: {
			close: function() {
				this.$emit('changeClose');
			},
			toJump(url) {
				if (!url) return
				uni.navigateTo({
					url
				});
			},
			base64ToBlob(base64Data) {
				const byteCharacters = atob(base64Data);
				const byteNumbers = new Array(byteCharacters.length);
				for (let i = 0; i < byteCharacters.length; i++) {
					byteNumbers[i] = byteCharacters.charCodeAt(i);
				}
				const byteArray = new Uint8Array(byteNumbers);
				try {
					// Check if Blob is available
					if (typeof Blob !== 'undefined') {
						return new Blob([byteArray], {
							type: 'image/png'
						}); // Change 'image/png' accordingly
					} else {
						throw new Error('Blob is not supported');
					}
				} catch (error) {
					console.error(error);
					// Handle the error accordingly
					return null;
				}
			},
			async agree() {
				console.log(this.shipid, '大萨达撒达到');
				// let blob = this.base64ToBlob(this.signImg)
				// console.log(blob,'保存签名');
				const res = await uploadImage(this.signImg)
				if (res.code == 1) {
					if (this.shipid > 0) {
						await agreeSign({
							'url': res.data.url,
							'ship_id': this.shipid
						}).then(res => {
							if (res.code == 1) {
								uni.showToast({
									'icon': 'none',
									'title': '签署成功,请再次点击接受',
									'duration': 1500
								});
							} else {
								uni.showToast({
									'icon': 'none',
									'title': '签署失败,稍后再试',
									'duration': 1500
								});
							}

						})
					} else {
						uni.showToast({
							'icon': 'none',
							'title': '签署成功,请选择',
							'duration': 1500
						});
					}

					uni.$emit('sign', res.data.url);
					this.$emit('changeClose');
				}
			},
			sign() {
				console.log('开始签名');
				uni.navigateTo({
					url: '/pages/love/sign'
				})
			},

		}
	}
</script>

<style scoped lang="scss">
	.address-window {
		background-color: #f6f6f6;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 9999;
		transform: translate3d(0, 100%, 0);
		transition: all .3s cubic-bezier(.25, .5, .5, .9);
		border-radius: 30rpx 30rpx 0 0;
		
	}

	.address-window.on {
		transform: translate3d(0, 0, 0);
	}

	.address-window .title {
		font-size: 28rpx;
		text-align: center;
		height: 100rpx;
		color: $uni-text-color-placeholder;
		line-height: 100rpx;
		position: relative;
	}

	.address-window .title .iconfont {
		position: absolute;
		right: 30rpx;
		top: 50%;
		transform: translate(0, -50%);
	}

	.address-window .box {
		padding: 45rpx 35rpx;
		width: 690rpx;
		height: 500rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin: 30rpx auto 100rpx;
	}

	.address-window .box .txt {
		font-weight: 600;
		font-size: 28rpx;
		line-height: 40rpx;
		color: #252525;
	}

	.address-window .box .sign {
		width: 100%;
		line-height: 208rpx;
		text-align: center;
		border-radius: 20rpx;
		border: 1px solid #DDDDDD;
		font-weight: bold;
		font-size: 60rpx;
		color: #CCCCCC;
		margin-top: 60rpx;
	}

	.signimg {
		width: 100%;
		line-height: 208rpx;
		text-align: center;
		border-radius: 20rpx;
		border: 1px solid #DDDDDD;
		font-weight: bold;
		font-size: 60rpx;
		color: #CCCCCC;
	}
</style>